package com.example.administrator.mysaft;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

public class ProcessmanagerSettingActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private  Switch mShowSysAppsTgb;
    private  Switch mKillProcessTgb;
    private  SharedPreferences msp;
    private  boolean running;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_processmanager_setting);
        msp=getSharedPreferences("config",MODE_PRIVATE);
        initView();
    }

    private void initView() {
        findViewById(R.id.rl_titlebar).setBackgroundColor(getResources().getColor(R.color.light_green));
        ImageView mLeftImgv= (ImageView) findViewById(R.id.imgv_leftbtn);
        mLeftImgv.setImageResource(R.drawable.login);
        mLeftImgv.setOnClickListener(this);
        ((TextView)findViewById(R.id.tv_title)).setText("进程管理设置");
        mShowSysAppsTgb= (Switch) findViewById(R.id.tgb_showsys_process);
        mKillProcessTgb= (Switch) findViewById(R.id.tgb_killprocess_lockscreen);
        mShowSysAppsTgb.setChecked(msp.getBoolean("showSystemProcess",true));
        running=SystemInfosUtils.isServiceRunning(this,"com.example.administrator.mysaft.AutoKillProcessService");
        mKillProcessTgb.setChecked(running);

        mKillProcessTgb.setOnCheckedChangeListener(this);
        mShowSysAppsTgb.setOnCheckedChangeListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imgv_leftbtn:
                finish();
                break;

        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.tgb_showsys_process:
                saveStatus("showSystemProcess",isChecked);
                break;
            case R.id.tgb_killprocess_lockscreen:
                Intent service=new Intent(this,AutoKillProcessService.class);
                if(isChecked){
                    startService(service);
                }else{
                    stopService(service);
                }
                break;
        }
    }

    private void saveStatus(String string, boolean isChecked) {
        SharedPreferences.Editor edit=msp.edit();
        edit.putBoolean(string,isChecked);
        edit.commit();

    }
}
